<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <link href="../static/css/bootstrap.css" rel="stylesheet" type="text/css"/>
    <link href="../static/css/components.min.css" rel="stylesheet" type="text/css"/>
    <link href="../static/css/login.css" rel="stylesheet" type="text/css"/>
    <script src="../static/js/jquery-1.11.2.min.js" type="text/javascript"></script>
</head>
<body class="login">
    <div class="content">
        <h3 class="form-title">用户注册</h3>
        <div class="form-group">
            <label class="control-label">手机号</label>
            <div>
                <input type="text" class="form-control" placeholder="手机号" name="telephone" id="telephone">
            </div>
            <button id="getOtp" class="btn blue" type="submit">获取OTP验证码</button>
        </div>
        <div class="form-group">
            <label class="control-label">验证码</label>
            <div>
                <input type="text" class="form-control" placeholder="验证码" name="otpCode" id="otpCode">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">用户昵称</label>
            <div>
                <input type="text" class="form-control" placeholder="用户昵称" name="name" id="name">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">性别</label>
            <div>
                <input type="text" class="form-control" placeholder="性别：男/女" name="gender" id="gender">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">年龄</label>
            <div>
                <input type="text" class="form-control" placeholder="年龄" name="age" id="age">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">密码</label>
            <div>
                <input type="password" class="form-control" placeholder="密码" name="password" id="password">
            </div>
        </div>
        <div class="form-actions">
            <button id="register" class="btn blue" type="submit">提交注册</button>
            <button id="login" class="btn green" type="submit">返回登录</button>
        </div>
    </div>
</body>

<script>
    // DOM加载完毕后再执行
    jQuery(document).ready(function(){
        // 绑定OTP的click点击事件，向后端发送请求
        $("#getOtp").on("click",function(){
            var telephone = $("#telephone").val();
            if (telephone == null || telephone == "") {
                alert("手机号不能为空");
                return false;
            }
            $.ajax({
                type: "POST",
                contentType: "application/x-www-form-urlencoded",
                url: "http://localhost:8888/getOtp",
                data: {
                    "telephone": telephone 
                },
                xhrFields: {
                    withCredentials: true
                },// 允许跨域的授信请求,配合后端的allowCredentials
                success: (res)=>{
                    if (res.status == "success") {
                        alert("OTP已经发送到您的手机上，请注意查收");
                    } else {
                        alert("OTP发送失败，原因为" + res.data.errorMessage);
                    }
                },
                error: (res)=>{
                    alert("OTP发送错误，原因为" + res.responseText);
                }
            });
            return false;
        }); 
        
        $("#register").on("click",function(){
            var telephone = $("#telephone").val();
            var otpCode = $("#otpCode").val();
            var name = $("#name").val();
            var gender = $("#gender").val();
            if (gender == "男") {
                gender = true;
            } else if(gender == "女") {
                gender = false;
            } else {
                alert("性别输入有误！男/女");
                return false;
            }
            var age = $("#age").val();
            var password = $("#password").val();

            $.ajax({
                type: "POST",
                contentType: "application/x-www-form-urlencoded;charset=utf-8",
                url: "http://localhost:8888/register",
                data: {
                    "telephone": telephone,
                    "otpCode": otpCode,
                    "name": name,
                    "gender": gender,
                    "age": age,
                    "password": password
                },
                xhrFields: {
                    withCredentials: true
                },// 允许跨域的授信请求,配合后端的allowCredentials
                success: (res)=>{
                    if (res.status == "success") {
                        alert("注册成功");
                    } else {
                        alert("注册失败，原因为：" + res.data.errorMessage);
                    }
                },
                error: (res)=>{
                    alert("注册错误，原因为：" + res.responseText);
                }
            });
            return false;
        });

        $("#login").on("click",function(){
            window.location.href="file:///E:/WorkSpace/Seckill/UI/templates/login.html";
        })
    });
</script>

</html>